const { Stack, Queue, Deque, StackQueueAlgorithms } = require("./template")

function testStackQueue() {
  // 测试栈的基本操作
  console.log(
    "1. 栈初始状态:",
    (() => {
      const stack = new Stack()
      return stack.isEmpty() === true && stack.size() === 0
    })()
  )

  console.log(
    "2. 栈入栈操作:",
    (() => {
      const stack = new Stack()
      stack.push(1)
      stack.push(2)
      return stack.size() === 2 && stack.peek() === 2
    })()
  )

  console.log(
    "3. 栈出栈操作:",
    (() => {
      const stack = new Stack()
      stack.push(1)
      stack.push(2)
      return stack.pop() === 2 && stack.pop() === 1 && stack.isEmpty() === true
    })()
  )

  console.log(
    "4. 栈LIFO特性:",
    (() => {
      const stack = new Stack()
      stack.push("A")
      stack.push("B")
      stack.push("C")
      return stack.pop() === "C" && stack.pop() === "B" && stack.pop() === "A"
    })()
  )

  console.log(
    "5. 栈边界情况:",
    (() => {
      const stack = new Stack()
      return stack.pop() === undefined && stack.peek() === undefined
    })()
  )

  // 测试队列的基本操作
  console.log(
    "6. 队列初始状态:",
    (() => {
      const queue = new Queue()
      return queue.isEmpty() === true && queue.size() === 0
    })()
  )

  console.log(
    "7. 队列入队操作:",
    (() => {
      const queue = new Queue()
      queue.enqueue(1)
      queue.enqueue(2)
      return queue.size() === 2 && queue.front() === 1
    })()
  )

  console.log(
    "8. 队列出队操作:",
    (() => {
      const queue = new Queue()
      queue.enqueue(1)
      queue.enqueue(2)
      return queue.dequeue() === 1 && queue.dequeue() === 2 && queue.isEmpty() === true
    })()
  )

  console.log(
    "9. 队列FIFO特性:",
    (() => {
      const queue = new Queue()
      queue.enqueue("A")
      queue.enqueue("B")
      queue.enqueue("C")
      return queue.dequeue() === "A" && queue.dequeue() === "B" && queue.dequeue() === "C"
    })()
  )

  console.log(
    "10. 队列边界情况:",
    (() => {
      const queue = new Queue()
      return queue.dequeue() === undefined && queue.front() === undefined
    })()
  )

  // 测试双端队列的基本操作
  console.log(
    "11. 双端队列初始状态:",
    (() => {
      const deque = new Deque()
      return deque.isEmpty() === true && deque.size() === 0
    })()
  )

  console.log(
    "12. 双端队列前后添加:",
    (() => {
      const deque = new Deque()
      deque.addFront(1)
      deque.addRear(2)
      return deque.size() === 2 && deque.peekFront() === 1 && deque.peekRear() === 2
    })()
  )

  console.log(
    "13. 双端队列前后移除:",
    (() => {
      const deque = new Deque()
      deque.addFront(1)
      deque.addRear(2)
      return deque.removeFront() === 1 && deque.removeRear() === 2 && deque.isEmpty() === true
    })()
  )

  console.log(
    "14. 双端队列边界情况:",
    (() => {
      const deque = new Deque()
      return deque.removeFront() === undefined && deque.removeRear() === undefined
    })()
  )

  // 测试算法
  const algorithms = new StackQueueAlgorithms()

  console.log(
    "15. 括号匹配正确情况:",
    (() => {
      return algorithms.isValidParentheses("()") === true &&
             algorithms.isValidParentheses("()[]{}") === true &&
             algorithms.isValidParentheses("{[]}") === true
    })()
  )

  console.log(
    "16. 括号匹配错误情况:",
    (() => {
      return algorithms.isValidParentheses("(]") === false &&
             algorithms.isValidParentheses("([)]") === false &&
             algorithms.isValidParentheses("((())") === false
    })()
  )

  console.log(
    "17. 用栈实现队列:",
    (() => {
      const stackQueue = new algorithms.StackQueue()
      stackQueue.enqueue(1)
      stackQueue.enqueue(2)
      return stackQueue.peek() === 1 && stackQueue.dequeue() === 1 && stackQueue.dequeue() === 2
    })()
  )

  console.log(
    "18. 用队列实现栈:",
    (() => {
      const queueStack = new algorithms.QueueStack()
      queueStack.push(1)
      queueStack.push(2)
      return queueStack.top() === 2 && queueStack.pop() === 2 && queueStack.pop() === 1
    })()
  )

  console.log(
    "19. 下一个更大元素:",
    (() => {
      const result = algorithms.nextGreaterElement([2, 1, 2, 4, 3, 1])
      const expected = [4, 2, 4, -1, -1, -1]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "20. 滑动窗口最大值:",
    (() => {
      const result = algorithms.maxSlidingWindow([1, 3, -1, -3, 5, 3, 6, 7], 3)
      const expected = [3, 3, 5, 5, 6, 7]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )
}

testStackQueue()
